package com.prs.crm.action.archives;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.archives.ArchivesManager;
import com.prs.crm.domain.archives.WaitBorrow;
import com.prs.crm.service.archives.ArchivesManagerService;
import com.prs.crm.service.archives.WaitBorrowService;

public class WaitBrrowAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private WaitBorrow waitBorrow;
	private WaitBorrowService waitBorrowService;
	private ArchivesManagerService archivesManagerService;
	public ArchivesManagerService getArchivesManagerService() {
		return archivesManagerService;
	}
	public void setArchivesManagerService(
			ArchivesManagerService archivesManagerService) {
		this.archivesManagerService = archivesManagerService;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public WaitBorrow getWaitBorrow() {
		return waitBorrow;
	}
	public void setWaitBorrow(WaitBorrow waitBorrow) {
		this.waitBorrow = waitBorrow;
	}
	public WaitBorrowService getWaitBorrowService() {
		return waitBorrowService;
	}
	public void setWaitBorrowService(WaitBorrowService waitBorrowService) {
		this.waitBorrowService = waitBorrowService;
	}
	/***********等待借阅***********/
	@Action("listWaitBorrow")
	public String listWaitBorrow(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(WaitBorrow.class);
		criteria.add(Restrictions.or(Restrictions.eq("state", 1), Restrictions.eq("state", 2)))
		.add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWaitBorrowService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	/*************等待审批*************/
	@Action("listWaitApp")
	public String listWaitApp(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(WaitBorrow.class);
		criteria.add(Restrictions.eq("state",1));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWaitBorrowService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action(value = "removeWaitBorrow", results = {
			@Result(name = "success", type = "redirect", location = "listWaitBorrow"),
			@Result(name = "error", type = "chain", location = "listWaitBorrow")
			})
	public String removeWaitBorrow(){
		if(ids!=null){
			this.waitBorrow=this.getWaitBorrowService().get(ids[0]);
		}
		this.getWaitBorrowService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Action(value = "saveAgree", results = {
			@Result(name = "success", type = "redirect", location = "listWaitApp"),
			@Result(name = "error", type = "chain", location = "listWaitApp")
			})
	public String saveAgree(){
		if(ids!=null){
			this.waitBorrow=this.getWaitBorrowService().get(ids[0]);
		}
		ArchivesManager am=this.getArchivesManagerService().getArchivesManager(waitBorrow.getNumber());
		if (am!=null) {
			am.setState(1);
			this.getArchivesManagerService().save(am);
		}
		WaitBorrow wb=this.getWaitBorrowService().getWaitBorrow(waitBorrow.getNumber(), 3);
		if (wb!=null) {
			this.addActionError("该档案已经被借出去，请等待归还后再借阅！");
			return ERROR;
		}
		waitBorrow.setState(3);
		waitBorrow.setApplyPerson(waitBorrow.getRecorder().getResume().getName());
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		waitBorrow.setApplyDate(sf.format(dt));
		this.getWaitBorrowService().save(waitBorrow);
		return SUCCESS;
	}
	
	@Action(value = "saveDisagree", results = {
			@Result(name = "success", type = "redirect", location = "listWaitApp"),
			@Result(name = "error", type = "chain", location = "listWaitApp")
			})
	public String saveDisagree(){
		if(ids!=null){
			this.waitBorrow=this.getWaitBorrowService().get(ids[0]);
		}
		waitBorrow.setState(2);
		waitBorrow.setApplyPerson(waitBorrow.getRecorder().getResume().getName());
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		waitBorrow.setApplyDate(sf.format(dt));
		this.getWaitBorrowService().save(waitBorrow);
		return SUCCESS;
	}
	@Action("viewWaitBorrow")
	public String viewWaitBorrow(){
		if(ids!=null){
			this.waitBorrow=this.getWaitBorrowService().get(ids[0]);
		}
		return SUCCESS;
	}
	
	
	/*************已借档案*************/
	@Action("listBorrow")
	public String listBorrow(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(WaitBorrow.class);
		criteria.add(Restrictions.eq("state",3));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWaitBorrowService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("listBorrows")
	public String listBorrows(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(WaitBorrow.class);
		criteria.add(Restrictions.eq("state",3)).add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWaitBorrowService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	/*************已归还档案*************/
	@Action("listRevert")
	public String listRevert(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(WaitBorrow.class);
		criteria.add(Restrictions.eq("state",4)).add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWaitBorrowService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action(value = "saveRevert", results = {
			@Result(name = "success", type = "redirect", location = "listBorrow"),
			@Result(name = "error", type = "chain", location = "listBorrow")
			})
	public String saveRevert(){
		if(ids!=null){
			this.waitBorrow=this.getWaitBorrowService().get(ids[0]);
		}
		ArchivesManager am=this.getArchivesManagerService().getArchivesManager(waitBorrow.getNumber());
		if (am!=null) {
			am.setState(0);
			this.getArchivesManagerService().save(am);
		}
		waitBorrow.setState(4);
		waitBorrow.setApplyPerson(waitBorrow.getRecorder().getResume().getName());
		Date dt=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		waitBorrow.setApplyDate(sf.format(dt));
		this.getWaitBorrowService().save(waitBorrow);
		return SUCCESS;
	}
	
	/*************已审批*************/
	@Action("listApprove")
	public String listApprove(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(WaitBorrow.class);
		criteria.add(Restrictions.or(Restrictions.or(Restrictions.eq("state", 2), Restrictions.eq("state", 3)), Restrictions.eq("state", 4)));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getWaitBorrowService().listByPage(criteria, pagination));
		return SUCCESS;
	}
}
